package com.guldan.jaina.task;

import com.guldan.core.constants.CommonConstant;
import com.guldan.jaina.common.task.BaseTask;
import com.guldan.jaina.dto.datastat.datav.DatavDTO;
import com.guldan.jaina.manager.ReportManager;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Silas.
 * Date: 2017/3/8
 * Time: 1:15
 */
@Component
public class CreateDatavDataTask extends BaseTask {

    @Autowired
    private ReportManager reportManager;

    @Override
    public void execute() throws Exception {

        Set<String> codeList = new HashSet<>();
//        codeList.add("630000");
        codeList.add("630100");

        long begin = System.currentTimeMillis();
        for (String code : codeList) {
            DatavDTO datavDTO = reportManager.getDatavDTO(code, false);

            try {
                logger.info(JSONObject.fromObject(datavDTO).toString());
                cacheService.putCache(String.format(CommonConstant.KEY_DATAV_AREA_DATA, datavDTO.getAreaId()), datavDTO);
            } catch (Exception ex) {
                logger.info("put datav to redis fail, datavDTO.getAreaId() %s", datavDTO.getAreaId());
            } finally {
                logger.error("CreateDatavDataTask execute cost " + (System.currentTimeMillis() - begin));
            }
        }
    }
}
